package Databases;

import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.exist.xmldb.XQueryService;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.PropertiesConfiguration;
import org.apache.commons.configuration.reloading.FileChangedReloadingStrategy;
import org.xmldb.api.base.*;
import org.xmldb.api.*;
import org.xmldb.api.modules.XUpdateQueryService;

public class ImpDBManager implements DBManager {

    private static final String PROPERTY_ACTIONS = "Action.properties";
    ImpXmlDbDataSource instanceDataSource = null;
    ImpDBActionResult results;
    final String XQueryService = "XQueryService";
    final String XQueryServiceVersion = "1.0";
    ArrayList<Object> listresult = new ArrayList();
    public static PropertiesConfiguration properties = null;

    private enum Actiontotake {

        SEARCH, UPDATE
    }

    private ImpDBManager() {
        try {
            instanceDataSource = new ImpXmlDbDataSource();
            results = new ImpDBActionResult();
            properties = new PropertiesConfiguration();
            properties.setFileName(PROPERTY_ACTIONS);
            properties.setReloadingStrategy(new FileChangedReloadingStrategy());
            properties.setDelimiterParsingDisabled(true);
            properties.load(ImpDBManager.class.getClassLoader().getResourceAsStream(PROPERTY_ACTIONS));
        } catch (ConfigurationException ex) {
            Logger.getLogger(ImpDBManager.class.getName()).log(Level.SEVERE, null, ex);
            results.setErrorMsg(ex.getMessage());
            results.setException(ex);
        } catch (XMLDBException ex) {
            Logger.getLogger(ImpDBManager.class.getName()).log(Level.SEVERE, null, ex);
            results.setErrorMsg(ex.getMessage());
            results.setException(ex);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ImpDBManager.class.getName()).log(Level.SEVERE, null, ex);
            results.setErrorMsg(ex.getMessage());
            results.setException(ex);
        } catch (InstantiationException ex) {
            Logger.getLogger(ImpDBManager.class.getName()).log(Level.SEVERE, null, ex);
            results.setErrorMsg(ex.getMessage());
            results.setException(ex);
        } catch (IllegalAccessException ex) {
            Logger.getLogger(ImpDBManager.class.getName()).log(Level.SEVERE, null, ex);
            results.setErrorMsg(ex.getMessage());
            results.setException(ex);
        }

    }

    public static ImpDBManager getInstance() {
        ImpDBManager instance = new ImpDBManager();
        return instance;
    }

    public static String getProperty(String actionprop) {
        return properties.getString(actionprop);
    }

    public DBActionResult execute(DBAction aDBAction) {
        String action = aDBAction.getActionId().substring(0, aDBAction.getActionId().indexOf("."));

        switch (Actiontotake.valueOf(action.toUpperCase())) {
            case SEARCH:
                MessageFormat formatt = new MessageFormat(ImpDBManager.getProperty(aDBAction.getActionId()));
                String[] actionproperties = (String[]) aDBAction.getParameters();
                String query = formatt.format(actionproperties);

                try {
                    XQueryService service = (XQueryService) instanceDataSource.getCollection().getService(XQueryService, XQueryServiceVersion);
                    ResourceSet resultset = service.query(query);
                    listresult.add(resultset);
                    results.setResult(listresult);
                } catch (XMLDBException ex) {
                    Logger.getLogger(ImpDBManager.class.getName()).log(Level.SEVERE, null, ex);
                    results.setErrorMsg(ex.getMessage());
                    results.setException(ex);
                }
                break;

            case UPDATE:
                MessageFormat formattt = new MessageFormat(ImpDBManager.getProperty(aDBAction.getActionId()));
                String[] actionpropertiess = (String[]) aDBAction.getParameters();
                String queryy = formattt.format(actionpropertiess);
                System.out.println(queryy);
                try {
                    XQueryService services = (XQueryService) instanceDataSource.getCollection().getService(XQueryService, XQueryServiceVersion);
                    services.query(queryy);

                } catch (XMLDBException ex) {
                    Logger.getLogger(ImpDBManager.class.getName()).log(Level.SEVERE, null, ex);
                }


                break;

            default:
                break;
        }
        return results;
    }
}
